<template>
  <el-container>
    <!-- 顶部 -->
    <el-header>
      <!-- 头部左侧 -->
      <div class="header_left">
        <img src="../assets/heima.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button @click="logout" class="btn">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isOpen ? '200px' : '70px'">
        <!-- 展开/收缩 -->
        <div class="open" @click="checkOpen">|||</div>

        <el-menu
          background-color="#343844"
          text-color="#fff"
          active-text-color="#3296f5"
          router
          unique-opened
          :default-active="currentActive"
          :collapse="!isOpen"
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.path"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- icon图标 -->
              <i :class="iconList[item.id]" class="icon"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="menu.path"
              v-for="menu in item.children"
              :key="menu.id"
              @click="changeCurrent(menu.path)"
            >
              <i class="el-icon-menu"></i>
              {{ menu.authName }}</el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
  <!-- <div>
    
  </div> -->
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      //  菜单列表
      menuList: [],
      // 当前激活的菜单
      currentActive: "",
      // 菜单栏是否为展开状态
      isOpen: true,
      // 根据菜单的id 设置icon
      iconList: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear();
      this.$message.info("您已退出登录");
      this.$router.push("/login");
    },
    // 获取菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");

      if (res.meta.status !== 200) this.$message.error("获取菜单数据失败");

      this.menuList = res.data;
    },
    // 改变选中的菜单
    changeCurrent(currentActive) {
      this.currentActive = currentActive;
      window.sessionStorage.setItem("currentActive", currentActive);
    },
    // 改变菜单展开状态
    checkOpen() {
      this.isOpen = !this.isOpen;
    },
  },
  mounted() {
    this.currentActive = window.sessionStorage.getItem("currentActive");
  },
};
</script>

<style scoped>
.el-container {
  height: 100%;
}
.el-header {
  height: 60px;
  background: #383f44;
  display: flex;
  justify-content: space-between;
}
.header_left {
  display: flex;
  align-content: center;
  color: #fff;
  font-size: 20px;
  line-height: 60px;
}
.header_left span {
  margin-left: 25px;
}
.btn {
  height: 40px;
  margin-top: 10px;
}

.el-aside {
  background: #343844;
}

.el-menu {
  border-right: 0;
}

/* 展开/收缩 */
.open {
  background: #464f60;
  color: #fff;
  text-align: center;
  height: 26px;
  line-height: 26px;
}

.icon {
  margin-right: 15px;
}
</style>